package com.sanshi.project.procure.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sanshi.project.admin.api.dto.ProcureGoodsTypeDTO;
import com.sanshi.project.admin.api.entity.ProcureGoodsTypeEntity;
import com.sanshi.project.common.core.util.R;
import com.sanshi.project.common.excel.annotation.ResponseExcel;
import com.sanshi.project.common.log.annotation.SysLog;
import com.sanshi.project.procure.service.ProcureGoodsTypeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 物品类型
 *
 * @author nikon
 * @date 2023-12-08 17:04:20
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/procureGoodsType")
@Tag(description = "procureGoodsType", name = "物品类型管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ProcureGoodsTypeController {

    private final ProcureGoodsTypeService procureGoodsTypeService;

    /**
     * 分页查询
     *
     * @param page             分页对象
     * @param procureGoodsType 物品类型
     * @return
     */
    @Operation(summary = "分页查询", description = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('procure_procureGoodsType_view')")
    public R getProcureGoodsTypePage(@ParameterObject Page page, @ParameterObject ProcureGoodsTypeDTO procureGoodsType) {
        LambdaQueryWrapper<ProcureGoodsTypeEntity> wrapper = Wrappers.lambdaQuery();
        // 物品类型名称
        wrapper.like(StrUtil.isNotBlank(procureGoodsType.getName()), ProcureGoodsTypeEntity::getName, procureGoodsType.getName());
        // 物品描述
        wrapper.like(StrUtil.isNotBlank(procureGoodsType.getDescription()), ProcureGoodsTypeEntity::getDescription, procureGoodsType.getDescription());
        wrapper.orderByDesc(ProcureGoodsTypeEntity::getUpdateTime);
        return R.ok(procureGoodsTypeService.page(page, wrapper));
    }


    /**
     * 通过id查询物品类型
     *
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('procure_procureGoodsType_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(procureGoodsTypeService.getById(id));
    }

    /**
     * 新增物品类型
     *
     * @param procureGoodsType 物品类型
     * @return R
     */
    @Operation(summary = "新增物品类型", description = "新增物品类型")
    @SysLog("新增物品类型")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('procure_procureGoodsType_add')")
    public R save(@RequestBody ProcureGoodsTypeEntity procureGoodsType) {
        return R.ok(procureGoodsTypeService.save(procureGoodsType));
    }

    /**
     * 修改物品类型
     *
     * @param procureGoodsType 物品类型
     * @return R
     */
    @Operation(summary = "修改物品类型", description = "修改物品类型")
    @SysLog("修改物品类型")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('procure_procureGoodsType_edit')")
    public R updateById(@RequestBody ProcureGoodsTypeEntity procureGoodsType) {
        return R.ok(procureGoodsTypeService.updateById(procureGoodsType));
    }

    /**
     * 通过id删除物品类型
     *
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除物品类型", description = "通过id删除物品类型")
    @SysLog("通过id删除物品类型")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('procure_procureGoodsType_del')")
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(procureGoodsTypeService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * 导出excel 表格
     *
     * @param procureGoodsType 查询条件
     * @param ids              导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('procure_procureGoodsType_export')")
    public List<ProcureGoodsTypeEntity> export(ProcureGoodsTypeEntity procureGoodsType, Long[] ids) {
        return procureGoodsTypeService.list(Wrappers.lambdaQuery(procureGoodsType).in(ArrayUtil.isNotEmpty(ids), ProcureGoodsTypeEntity::getId, ids));
    }

    /**
     * 物品类型全量查询
     *
     * @param procureGoodsType 物品类型
     * @return
     */
    @Operation(summary = "物品类型全量查询", description = "物品类型全量查询")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('procure_procureGoodsType_view')")
    public R getProcureGoodsTypeList(@ParameterObject ProcureGoodsTypeDTO procureGoodsType) {
        LambdaQueryWrapper<ProcureGoodsTypeEntity> wrapper = Wrappers.lambdaQuery();
        // 物品类型名称
        wrapper.like(StrUtil.isNotBlank(procureGoodsType.getName()), ProcureGoodsTypeEntity::getName, procureGoodsType.getName());
        // 物品描述
        wrapper.like(StrUtil.isNotBlank(procureGoodsType.getDescription()), ProcureGoodsTypeEntity::getDescription, procureGoodsType.getDescription());
        return R.ok(procureGoodsTypeService.list(wrapper));
    }
}